﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using IM.Common;

namespace IMClient
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("start client");
            TcpClient tclient = new TcpClient();
            IPAddress ipAddr = IPAddress.Parse("127.0.0.1");
            try
            {
                tclient.Connect(ipAddr, 5099);
                
                NetworkStream stream = tclient.GetStream();
                c2s_account_verify acountverify = new c2s_account_verify();

                System.Console.WriteLine("please input account");
                acountverify.szAccount = System.Console.ReadLine();

                System.Console.WriteLine("please input password");
                acountverify.szPassword = System.Console.ReadLine(); 
                byte[] bdata = acountverify.ToBytes();

                stream.Write(bdata, 0, bdata.Length);

                byte[] buf = new byte[1024];
                stream.Read(buf, 0, 1024);

                s2c_account_verify_result account_verify_result = new s2c_account_verify_result();
                account_verify_result.FromBytes(buf);

                if (account_verify_result.nVerifyResult == (int)IMResult.im_success)
                {
                    System.Console.WriteLine("login success\n\n");
                    System.Console.WriteLine("account defailed:");
                    System.Console.WriteLine("account :" + account_verify_result.szAccount);
                    System.Console.WriteLine("petName : " + account_verify_result.szPetName);
                    System.Console.WriteLine("sex : " + account_verify_result.nSex);
                    System.Console.WriteLine("birthday : " + account_verify_result.nYear + " " + account_verify_result.nMonth + " " + account_verify_result.nDay);
                    System.Console.WriteLine("last IP: " + account_verify_result.szLastLoginIP);
                    System.Console.WriteLine("motto: " + account_verify_result.szMotto);
                }
                else
                {
                    System.Console.WriteLine("login failed"); 
                }
                string strfriend;
                System.Console.WriteLine("please input friend");
                strfriend = System.Console.ReadLine();
                while (true)
                {
                    string strSendMsg;
                    System.Console.WriteLine("please input msg to send");
                    strSendMsg = System.Console.ReadLine();
                    c2s_account_chat account_chat = new c2s_account_chat();
                    account_chat.szAccount = strfriend;
                    account_chat.szContent = strSendMsg;

                    bdata = account_chat.ToBytes();
                    stream.Write(bdata, 0, bdata.Length);

                    stream.Read(buf, 0, 1024);
                    s2c_account_chat account_recv_chat = new s2c_account_chat();
                    account_recv_chat.FromBytes(buf);

                    System.Console.WriteLine("recv");
                    System.Console.WriteLine("  {0}", account_recv_chat.szContent);
                }
                System.Console.ReadKey();
               
            }
            catch (Exception e)
            {
                System.Console.WriteLine("connect failed");
            }
            System.Console.ReadKey();

        }
    }
}
